package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.domain.TCjtzjbPurchaseArrival;
import com.mjf.cjt.domain.TCjtzjbPurchasearrivalDetail;
import com.mjf.cjt.mapper.TCjtzjbPurchaseArrivalMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.ITCjtzjbPurchasearrivalDetailService;
import com.mjf.cjt.service.TCjtzjbPurchaseArrivalService;
import com.mjf.cjt.vo.PushArrivalCreateVO;
import com.mjf.cjt.vo.TFResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/8/6 22:21
 */
@Slf4j
@Service
public class TCjtzjbPurchaseArrivalServiceImpl extends ServiceImpl<TCjtzjbPurchaseArrivalMapper, TCjtzjbPurchaseArrival> implements TCjtzjbPurchaseArrivalService {


    @Autowired
    private ITCjtzjbPurchasearrivalDetailService itCjtzjbPurchasearrivalDetailService;
    @Autowired
    private AsyncTMService asyncTMService;

    @Override
    public void asyncPushProductReceive(String start, String end) {
        List<TCjtzjbPurchaseArrival> list = list(Wrappers.<TCjtzjbPurchaseArrival>lambdaQuery().eq(TCjtzjbPurchaseArrival::getIsPush, 0));
        pushProductReceive(list);
    }

    @Override
    public void asyncPushProductReceiveById(Long id) {
        TCjtzjbPurchaseArrival purchaseArrival = getById(id);
        pushProductReceive(Collections.singletonList(purchaseArrival));
    }

    public void pushProductReceive(List<TCjtzjbPurchaseArrival> list) {
        if (CollectionUtil.isNotEmpty( list)) {
            return;
        }
        // 查询进货单明细
        List<TCjtzjbPurchasearrivalDetail> detailList =
            itCjtzjbPurchasearrivalDetailService.list(Wrappers.<TCjtzjbPurchasearrivalDetail>lambdaQuery()
                .in(TCjtzjbPurchasearrivalDetail::getPurchaseId, list.stream()));
        Map<Long, List<TCjtzjbPurchasearrivalDetail>> detailMap =
            detailList.stream().collect(Collectors.groupingBy(TCjtzjbPurchasearrivalDetail::getPurchaseId));
        for (TCjtzjbPurchaseArrival arrival : list) {
            PushArrivalCreateVO vo = PushArrivalCreateVO.builder().build();
            PushArrivalCreateVO.Dto dto = PushArrivalCreateVO.Dto.builder().build();
            dto.setExternalCode(arrival.getExternalCode());
            dto.setCode(arrival.getCode());
            dto.setPartner(PushArrivalCreateVO.Partner.builder().Code(arrival.getPartnerCode()).build());
            dto.setWarehouse(PushArrivalCreateVO.Warehouse.builder().Code(arrival.getWarehouseCode()).Name(arrival.getWarehouseName()).build());
            dto.setBusinessType(PushArrivalCreateVO.BusinessType.builder().Code(arrival.getBusinesstypeCode()).build());
            dto.setVoucherDetails(detailMap.get(arrival.getId()).stream().map(e -> PushArrivalCreateVO.Details.builder()
                .Code(e.getInventoryCode())
                .Inventory(PushArrivalCreateVO.Inventory.builder()
                    .Code(e.getInventoryCode())
                    .Name(e.getInventoryName())
                    .build())
                .Quantity(e.getQuantity())
                .build()).collect(Collectors.toList()));
            vo.setDto(dto);
            try {
                TFResultVO resultVO = asyncTMService.pushArrivalCreate(vo);
                arrival.setIsPush(1);
                arrival.setPushResult(resultVO.getResult());
                arrival.setTimes(LocalDateTime.now());
            } catch (ChanjetApiException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
